package com.swt.gap.controller;

import com.swt.gap.common.RoleEnum;
import com.swt.gap.dao.SysRoleMapper;
import com.swt.gap.model.entity.SysRole;
import com.swt.gap.model.entity.SysUser;
import com.swt.gap.model.vo.LoginSuccessVO;
import com.swt.gap.model.vo.LoginVO;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/token")
@Api(tags = "100.用户登录登出接口")
public class PublicController {



    private final SysRoleMapper sysRoleMapper;

    public PublicController(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    /**
     * 登录接口
     * @param userQuery
     * @return
     */

    @PostMapping("login")
    @ApiOperation(notes = "登录",value = "登录")
    @ApiResponses({@ApiResponse(code = 201, message = "用户列表",response = LoginSuccessVO.class)})
    public ResponseEntity login(@RequestBody LoginVO userQuery){


        Subject subject = SecurityUtils.getSubject();
        LoginSuccessVO info = new LoginSuccessVO();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userQuery.getUserName(), userQuery.getPassword());
            subject.login(usernamePasswordToken);
            String token = (String) subject.getSession().getId();
            info.setToken(token);
            SysUser sysUser = (SysUser) subject.getPrincipal();
            sysUser.setPassword(null);
            BeanUtils.copyProperties(sysUser,info);
            SysRole sysRole = sysRoleMapper.selectRoleByUserId(sysUser.getUserId());
            if (null != sysRole) {
                info.setRoleId(sysRole.getRoleId());
                info.setRoleName(sysRole.getRoleName());
                switch (RoleEnum.getByCode(sysRole.getRoleId())) {
                    case SUPER:
                    case ADMIN:
                        info.setHasAdminPermission(true);
                        break;
                    default:
                        info.setHasAdminPermission(false);
                        break;
                }
            }
            return ResponseEntity.status(HttpStatus.CREATED).body(info);
        }/*catch (ExcessiveAttemptsException e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("输入错误密码超过3次，请5分钟之后再登陆");
        } */catch (AuthenticationException e2){
            e2.printStackTrace();
            if("账号未启用".endsWith(e2.getMessage())) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("账号未启用");
            }
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("账号或者密码错误");
        }
    }

    @GetMapping("/logout")
    @ApiOperation(notes = "登出",value = "登出")
    public ResponseEntity lout(){
        Subject subject = SecurityUtils.getSubject();
        if (subject.getPrincipals()!=null){
            SecurityUtils.getSubject().logout();
        }
        return ResponseEntity.ok("登出成功！");

    }
    @ApiIgnore
    @RequestMapping("need_login")
    public ResponseEntity needLogin(){

        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("温馨提示：请使用对应的账号登录");
//        return ResponseData.error(ResponseCode.NOT_SIGN_IN_CODE,"温馨提示：请使用对应的账号登录");

    }
    @ApiIgnore
    @GetMapping("not_permit")
    public ResponseEntity notPermit(){

        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("温馨提示：拒绝访问，没权限");
    }


    @GetMapping("token")
    @ApiOperation(value = "测试", notes = "2。8测试")
    public ResponseEntity test(HttpServletRequest request) {
//        Subject subject = SecurityUtils.getSubject();
//        SysUser sysUser = (SysUser) subject.getPrincipal();
       SysUser sysUser = UserLoginUtils.getUser();
        System.out.println(sysUser);
        return ResponseEntity.ok(sysUser);
    }
}
